<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
require_once("lib/validate.php");
//Includes
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("components4phpfull/jtdivwindow.inc.php");
use_unit("components4phpfull/jtgroupbox.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("components4phpfull/jtiframe.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class register_company_edit2 extends Page
{
       public $Label9 = null;
       public $lblstreet = null;
       public $lblCity = null;
       public $edCity = null;
       public $Label10 = null;
       public $edAddress = null;
       public $Label24 = null;
       public $hfOther = null;
       public $Label17 = null;
       public $edOther = null;
       public $lblfcode = null;
       public $Label16 = null;
       public $edFcode = null;
       public $Label15 = null;
       public $cmbCountry = null;
       public $imgBack = null;
       public $cmbbtype = null;
       public $lblarea = null;
       public $lblPhone = null;
       public $edArea = null;
       public $JSValidate = null;
       public $JSProvinceMunicipality = null;
       public $JSRegionProvince = null;
       public $Label8 = null;
       public $hfError = null;
       public $lblemail = null;
       public $lblcomp = null;
       public $compid = null;
       public $JTProd1 = null;
       public $JTProd2 = null;
       public $JTProd3 = null;
       public $btnShowProduct = null;
       public $JTDivProduct = null;
       public $JTGroupBox3 = null;
       public $btnAddProduct = null;
       public $btnDelProduct = null;
       public $edProduct = null;
       public $listProduct = null;
       public $cmbProduct = null;
       public $Label2 = null;
       public $cmbIndustry = null;
       public $hfAlert = null;
       public $imgNext = null;
       public $btnAddG = null;
       public $Label14 = null;
       public $Image3 = null;
       public $JTBranch = null;
       public $JTSiteTheme1 = null;
       public $Button1 = null;
       public $Label12 = null;
       public $hiddenAuto = null;
       public $hiddenNewCount = null;
       public $hiddenExist = null;
       public $hiddenComp = null;
       public $Label4 = null;
       public $Image2 = null;
       public $JTContact = null;
       public $edApnum = null;
       public $Label3 = null;
       public $Label29 = null;
       public $edcompphone = null;
       public $Label23 = null;
       public $edcompfax = null;
       public $uploadLogo = null;
       public $lblPhoto = null;
       public $Label11 = null;
       public $edBtype = null;
       public $Label7 = null;
       public $Label5 = null;
       public $edWebsite = null;
       public $edEmail = null;
       public $Label13 = null;
       public $Label28 = null;
       public $cmbMunicipality = null;
       public $Label27 = null;
       public $cmbProvince = null;
       public $Label26 = null;
       public $cmbRegion = null;
       public $edBldg = null;
       public $Label25 = null;
       public $lblError = null;
       public $edCompanyName = null;
       public $Label1 = null;
       public $cmbBusiness = null;
       public $Label6 = null;
       public $edSlogan = null;
       public $imgChangePw = null;
       public $Image1 = null;
       function edAddressJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edAddress','lblstreet');
       <?php

       }

       function edCityJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edCity','lblCity');
       <?php

       }

       function edFcodeJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       var edArea = document.getElementById('edFcode').value;
       //validateRequire('edArea','lblarea');

       if(edArea.length>0)
       {
       regex_pattern = "^[0-9]{2,4}$";
       var reg = new RegExp(regex_pattern);
       var match = document.getElementById('edFcode').value.match(reg);
       if(match==null)
       {
        document.getElementById('lblfcode').innerHTML = "&darr;&nbsp;Incorrect format - e.g. 02&nbsp;&darr;";
       }
       else
       {
        document.getElementById('lblfcode').innerHTML = "&nbsp;";
       }
       }
       <?php

       }

       function cmbbtypeJSChange($sender, $params)
       {

       ?>
       //Add your javascript code here
       var btype = document.getElementById('cmbbtype').value;
       var other = document.getElementById('hfOther').value;

       if(btype=='Other')
       {
       //alert(btype);
       document.getElementById("edOther").disabled=false;
       document.getElementById("edOther").style.backgroundColor='#000000';
       document.getElementById("edOther").value = other;
       }
       else
       {
       //alert(btype);
       document.getElementById("edOther").disabled=true;
       document.getElementById("edOther").style.backgroundColor='#707070';
       document.getElementById("edOther").value = '';
       }
       <?php

       }

       function imgBackJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       window.location.href = "set_wizard.php";
       <?php

       }

       function imgBackJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgBack'].src="image/btn_back1.png"
       <?php

       }

       function imgBackJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgBack'].src="image/btn_back21.png"
       <?php

       }

       function edAreaJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       var edArea = document.getElementById('edArea').value;
       validateRequire('edArea','lblarea');

       if(edArea.length>0)
       {
       regex_pattern = "^[0-9]{2,4}$";
       var reg = new RegExp(regex_pattern);
       var match = document.getElementById('edArea').value.match(reg);
       if(match==null)
       {
        document.getElementById('lblarea').innerHTML = "&darr;&nbsp;Incorrect format - e.g. 02&nbsp;&darr;";
       }
       else
       {
        document.getElementById('lblarea').innerHTML = "&nbsp;";
       }
       }
       <?php

       }

       function edEmailJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edEmail','lblemail');

       var edEmail = document.getElementById('edEmail').value;

       if(edEmail.length>0)
       {
       regex_pattern = "[A-Za-z0-9_\.]+[@][A-Za-z-]+[.][A-Za-z]{2,4}([\.A-Za-z]{2,3})?$";
       var regm = new RegExp(regex_pattern);
       var match = document.getElementById('edEmail').value.match(regm);
       if(match==null)
       {
        document.getElementById('lblemail').innerHTML = "&darr;&nbsp;Incorrect format - e.g.(user@mail.com)&nbsp;&darr;";
       }
       }
       <?php

       }

       function edcompphoneJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edcompphone','lblPhone');

       var edcompphone = document.getElementById('edcompphone').value;

       if(edcompphone.length>0)
       {
       regex_pattern = "^[0-9]{3}[-]?[0-9]{4}$";
       var reg = new RegExp(regex_pattern);
       var match = document.getElementById('edcompphone').value.match(reg);
       if(match==null)
       {
        document.getElementById('lblPhone').innerHTML = "&darr;&nbsp;Incorrect format - e.g.899-1234&nbsp;&darr;";
       }
       else
       {
        document.getElementById('lblPhone').innerHTML = "";
       }
       }
       <?php

       }

       function edCompanyNameJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edCompanyName','lblcomp');
       <?php

       }


       function btnDelProductJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var ProductIndex = document.getElementById('listProduct').selectedIndex;
       var ProductValue = document.getElementById('listProduct')[ProductIndex].value;
       var comp = document.getElementById('compid').value;
       AjaxProd3(ProductValue,comp);
       <?php

       }

       function btnAddProductJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var addproduct = document.getElementById('edProduct').value;
       var comp = document.getElementById('compid').value;
       AjaxProd2(addproduct,comp);
       document.getElementById('edProduct').value = "";
       <?php

       }

       function btnShowProductJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('JTDivProduct').Show();
       doucment.getElementById('edProduct').value = "";
       <?php

       }

       function register_company_edit2JSLoad($sender, $params)
       {

       ?>
       var btype = document.getElementById('cmbbtype').value;
       var other = document.getElementById('hfOther').value;

       if(btype=='Other')
       {
       //alert(btype);
       document.getElementById("edOther").disabled=false;
       document.getElementById("edOther").style.backgroundColor='#000000';
       document.getElementById("edOther").value = other;
       }
       else
       {
       //alert(btype);
       document.getElementById("edOther").disabled=true;
       document.getElementById("edOther").style.backgroundColor='#707070';
       document.getElementById("edOther").value = '';
       }
       <?php

       }


       function imgNextJSMouseOut($sender, $params)
       {

       ?>
       document.images['imgNext'].src = 'image/btn_next.png';

       <?php

       }

       function imgNextJSMouseOver($sender, $params)
       {

       ?>
       document.images['imgNext'].src = 'image/btn_next2.png';

       <?php

       }

       function imgNextClick($sender, $params)
       {
       $this->validateRequired($this->edCompanyName->Text, 'lblcomp');
       $this->validateRequired($this->edAddress->Text, 'lblstreet');

       $phone = $this->edcompphone->Text;
       $this->validateRequired($this->edcompphone->Text, 'lblPhone');

       $city = $this->edCity->Text;
       $this->validateRequired($this->edCity->Text, 'lblCity');

       if($phone!='')
       {
       if(preg_match("/^[0-9]{3}[-]?[0-9]{4}$/", $phone))
       {
       $this->lblPhone->Caption = '';
       }
       else
       {
       $this->lblPhone->Caption = '&darr;&nbsp;Incorrect format - e.g.899-1234&nbsp;&darr;';
       $this->hfError->setValue('1');
       }
       }

       $acode = $this->edArea->Text;
       $this->validateRequired($this->edArea->Text, 'lblarea');

       if($acode!='')
       {
       if(preg_match("/^[0-9]{2,4}$/", $acode))
       {
       $this->lblarea->Caption = '';
       }
       else
       {
       $this->lblarea->Caption = 'Incorrect format - e.g.02&nbsp;&darr;';
       $this->hfError->setValue('1');
       }
       }


       $email = $this->edEmail->Text;
       if($email=='')
       $this->validateRequired($this->edEmail->Text, 'lblemail');

       if($email!='')
       {
       if(preg_match("/[A-Za-z0-9_\.]+[@][A-Za-z-]+[.][A-Za-z]{2,4}([\.A-Za-z]{2,3})?$/", $email))
       {
       $this->lblemail->Caption = '';
       }
       else
       {
       $this->lblemail->Caption = '&darr;&nbsp;Incorrect format - e.g.(user@mail.com)&nbsp;&darr;';
       $this->hfError->setValue('1');
       }
       }

       $error = $this->hfError->getValue();
       //die($error);
       if($error == '1' || $number != $key)
       {
          $this->hfError->setValue('0');
          return false;
       }
       else if($error == '0')
       {
       $result_upload = mysql_query("select * from company_tbl where company_id='".$_SESSION['company_id']."'") or die('EDIT COMPANY'.mysql_error());
       $row_upload = mysql_fetch_array($result_upload);

       /*if($this->Upload1->FileName=='' || $this->Upload1->FileName == null)
       {
        $query_photo = '';
       }
       else
       {
       $query_photo = ",photo='"."photo/company/logo".$_SESSION['company_id'].".".$this->Upload1->FileExt."'";
       $this->Upload1->moveUploadedFile("photo/company/logo".$_SESSION['company_id'].".".$this->Upload1->FileExt);
       }*/
       if($this->uploadLogo->FileExt=="")
       {
       $query_update = "update company_tbl set
       company_name='".$this->edCompanyName->Text."',
       address='".$this->edAddress->Text."',
       bldg='".$this->edBldg->Text."',
       city='".$this->edCity->Text."',
       country='".$this->cmbCountry->getItemIndex()."',
       business_type='".$this->cmbbtype->getItemIndex()."',
       other = '".$this->edOther->Text."',
       industry='".$this->cmbIndustry->getItemIndex()."',
       website='".$this->edWebsite->Text."',
       acode='".$this->edArea->Text."',
       comp_phone='".$this->edcompphone->Text."',
       acode_fax = '".$this->edFcode->Text."',
       comp_fax='".$this->edcompfax->Text."',
       general_email='".$this->edEmail->Text."'
       where company_id='".$_SESSION['company_id']."'";
       mysql_query($query_update) or die(mysql_error());
       }

       if($this->uploadLogo->FileExt!="")
       {
       $query_update = "update company_tbl set
       company_name='".$this->edCompanyName->Text."',
       address='".$this->edAddress->Text."',
       bldg='".$this->edBldg->Text."',
       city='".$this->edCity->Text."',
       country='".$this->cmbCountry->getItemIndex()."',
       business_type='".$this->cmbbtype->getItemIndex()."',
       other = '".$this->edOther->Text."',
       industry='".$this->cmbIndustry->getItemIndex()."',
       website='".$this->edWebsite->Text."',
       acode='".$this->edArea->Text."',
       comp_phone='".$this->edcompphone->Text."',
       acode_fax = '".$this->edFcode->Text."',
       comp_fax='".$this->edcompfax->Text."',
       general_email='".$this->edEmail->Text."',
       photo='"."photo/company/logo".$_SESSION['company_id'].".".$this->uploadLogo->FileExt."'
       where company_id='".$_SESSION['company_id']."'";
       mysql_query($query_update) or die(mysql_error());
       }

       $this->uploadLogo->moveUploadedFile("photo/company/logo".$_SESSION['company_id'].".".$this->uploadLogo->FileExt);
       //redirect('vehicle_list.php?company_id='.$_SESSION['company_id']);
       echo "<script type=text/javascript>
       document.location.href='set_standard_droplist.php';
       </script>";
       }

       }

       function btnAddGJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var comp = document.getElementById("hiddenComp").value;
       var win_height = 304;
       var win_width = 368;
       var height = window.opener.outerHeight;
       var width = window.opener.outerWidth;
       var top = Math.round((height-win_height)/2);
       var left = Math.round((width-win_width)/2);
       window.open("comp_garage_create.php?comp_id="+comp,'add_branch','location=no,height='+win_height+',width='+win_width+',top='+top+',left='+left);
       return false;
       <?php

       }

       function Button1JSClick($sender, $params)
       {

       ?>
       var comp = document.getElementById("hiddenComp").value;
       var win_height = 364;
       var win_width = 744;
       var height = window.opener.outerHeight;
       var width = window.opener.outerWidth;
       var top = Math.round((height-win_height)/2);
       var left = Math.round((width-win_width)/2);
       window.open("comp_contact_create.php?comp_id="+comp,"Window2",'location=no,height='+win_height+',width='+win_width+',top='+top+',left='+left);
       return false;
       <?php

       }

       function cmbProvinceJSChange($sender, $params)
       {
       //echo $this->cmbProvince->ajaxCall("VendorProvinceChange");
       ?>
       //Add your javascript code here
       provinceMunicipality();
       return false;
       <?php

       }

       function cmbRegionJSChange($sender, $params)
       {
       //echo $this->cmbRegion->ajaxCall("VendorRegionChange");
       ?>
       //Add your javascript code here
       regionProvince();
       //provinceMunicipality();
       return false;
       <?php

       }


       function imgChangePwJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgChangePw'].src="image/change_password1.png"
       <?php

       }

       function imgChangePwJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgChangePw'].src="image/change_password_red1.png"
       <?php

       }

       function imgChangePwJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       window.open('change_password.php', 'popup', 'width=424, left=450, top=250, height=192')
       <?php

       }

       function lblChangePwJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       window.open('change_password.php', 'popup', 'width=424, height=192')
       <?php

       }

        function VendorRegionChange()
       {
       $region_id = $this->cmbRegion->getItemIndex();
       $this->cmbProvince->Clear();
       $this->cmbProvince->AddItem('--Select--', null, null);
       $result_province = mysql_query("select * from lookup_province where region_id='".$region_id."'");

       while($row_province = mysql_fetch_array($result_province))
       {
            $this->cmbProvince->AddItem($row_province['province'], null, $row_province['id']);
       }

       if($region_id==0)
       {
        $this->cmbProvince->Clear();
        $this->cmbMunicipality->Clear();
        $this->cmbProvince->AddItem('--Select Region First--',null,null);
        $this->cmbMunicipality->AddItem('--Select Province First--',null,null);
       }
       }



       function VendorProvinceChange()
       {
       $region_id = $this->cmbRegion->getItemIndex();
       $this->cmbProvince->Clear();
       $this->cmbProvince->AddItem('--Select--', null, null);
       $result_province = mysql_query("select * from lookup_province where region_id='".$region_id."'");

       while($row_province = mysql_fetch_array($result_province))
       {
            $this->cmbProvince->AddItem($row_province['province'], null, $row_province['id']);
       }

       $province_id = $this->cmbProvince->getItemIndex();
       $this->cmbMunicipality->Clear();
       $this->cmbMunicipality->AddItem('--Select--', null, null);
       $query_municipality = "select * from lookup_municipality where province_id='".$province_id."'";
       $result_municipality = mysql_query($query_municipality);
       //die($query_municipality);
       while($row_municipality = mysql_fetch_array($result_municipality))
       {
            $this->cmbMunicipality->AddItem($row_municipality['municipality'], null, $row_municipality['id']);
       }

       if($province_id==0)
       {
        $this->cmbMunicipality->Clear();
        $this->cmbMunicipality->AddItem('--Select Province First--',null,null);
       }
       }

        function validateRequired($value, $lbl)
       {
          if($value == '' || $value == null)
          {
             $this->$lbl->Caption = '&darr; This field is required &darr;';
             $this->hfError->setValue('1');
          }
       }



       function register_company_edit2Create($sender, $params)
       {
       $this->hfError->setValue(0);
       //if($_GET['alert']==1)
       //echo "<script type=text/javascript>alert('Welcome to AMPS! Please complete your profile and configure settings first to continue.');</script>";
       $alert = $_GET['alert'];
       if($alert!='' && $alert!=null)
       {
        $this->hfAlert->setValue($alert);
       }
       $comp_id = $_SESSION['company_id'];
       $this->hiddenComp->setValue($comp_id);
       $this->compid->Value = $_SESSION['company_id'];
       $result_company = mysql_query("select * from company_tbl where company_id='".$_SESSION['company_id']."'") or die('EDIT COMPANY'.mysql_error());
       $row_company = mysql_fetch_array($result_company);

       //start for validation
       $this->lblPhone->Caption = '';
       $this->lblemail->Caption = '';
       $this->lblcomp->Caption = '';
       $this->lblarea->Caption = '';
       $this->lblfcode->Caption = '';
       $this->lblstreet->Caption = '';
       $this->lblCity->Caption = '';
       //end for validation



       $this->JTContact->setURL('set_comp_contact_list.php?company_id='.$_SESSION['company_id']);
       $this->JTBranch->setURL('set_comp_garage_list.php?company_id='.$_SESSION['company_id']);
       //$this->lblChangePw->setLink('change_password.php');

       $this->edCompanyName->Text = $row_company['company_name'];
       $this->edApnum->Text = $row_company['app_num'];
       $this->edAddress->Text = $row_company['address'];
       $this->edBldg->Text = $row_company['bldg'];
       $this->edWebsite->Text = $row_company['website'];
       $this->edEmail->Text = $row_company['general_email'];
       $this->edArea->Text = $row_company['acode'];
       $this->edcompphone->Text = $row_company['comp_phone'];
       $this->edcompfax->Text = $row_company['comp_fax'];
       $this->cmbbtype->ItemIndex = $row_company['business_type'];
       $this->cmbIndustry->ItemIndex = $row_company['industry'];
       $this->cmbProduct->ItemIndex = $row_company['products'];
       $this->edCity->Text = $row_company['city'];
       $this->cmbCountry->ItemIndex = $row_company['country'];
       $this->edOther->Text = $row_company['other'];
       $this->edFcode->Text = $row_company['acode_fax'];

       $other = $row_company['other'];
       $this->hfOther->setValue($other);
       //$this->edTaxIdNum->Text = $row_company['tax_idno'];
       //$this->edAddress2->Text = $row_company['address2'];
       //$this->edLname->Text = $row_company['lname'];
       //$this->edFname->Text = $row_company['fname'];
       //$this->edMname->Text = $row_company['mname'];
       //$this->edMobile->Text = $row_company['mobile'];
       //$this->edPhone->Text = $row_company['contact_no'];
       //$this->edFax->Text = $row_company['fax_no'];
       //$this->edSlogan->Text = $row_company['comp_slogan'];
       //$this->edcontactemail1->Text = $row_company['email1'];
       //$this->edcontactemail2->Text = $row_company['email2'];
       //$this->edposition1->Text = $row_company['position1'];
       //$this->edposition2->Text = $row_company['position2'];
       //$this->edlname2->Text = $row_company['lname2'];
       //$this->edfname2->Text = $row_company['fname2'];
       //$this->edmname2->Text = $row_company['mname2'];
       //$this->edphone2->Text = $row_company['phone2'];
       //$this->edfax2->Text = $row_company['fax2'];
       //$this->edmobile2->Text = $row_company['mobile2'];
       /*$result_account = mysql_query("select * from accounts_tbl where username='".$_SESSION['username']."'") or die('SELECT ACCOUNTS'.mysql_error());
       $row_account = mysql_fetch_array($result_account);

       $this->edUsername->Text = $row_account['username'];
       $this->edPassword->Text = $row_account['password'];
       $this->edPassword2->Text = $row_account['password'];*/
       $this->cmbCountry->Clear();
        $result_country = mysql_query("select * from countries order by country ASC");

        while($row_country = mysql_fetch_array($result_country))
        {
            $this->cmbCountry->AddItem($row_country['country'], null, $row_country['country']);
        }




       $this->cmbProduct->Clear();
        $this->cmbProduct->AddItem('--Select--', null, null);
        $result_style = mysql_query("select * from lookup_inventory_tbl where company_id = '".$_SESSION['company_id']."' AND category='types'");

        while($row_style = mysql_fetch_array($result_style))
        {
            $this->cmbProduct->AddItem($row_style['value'], null, $row_style['value']);
        }

        //list product
        $this->listProduct->Clear();
        $result_product = mysql_query("select * from lookup_inventory_tbl where company_id = '".$_SESSION['company_id']."' AND category='types'");

        while($row_product = mysql_fetch_array($result_product))
        {
            $this->listProduct->AddItem($row_product['value'], null, $row_product['value']);
        }


       //region,province,municipality
       $this->cmbRegion->Clear();
       $this->cmbRegion->AddItem('--Select--', null, null);
       $this->cmbRegion->ItemIndex=$row_company['region'];
       $result_vend_region = mysql_query("select * from lookup_region");

        while($row_vend_region = mysql_fetch_array($result_vend_region))
        {
            $this->cmbRegion->AddItem($row_vend_region['region'], null, $row_vend_region['id']);
        }

       $region_id = $row_company['region'];
       $this->cmbProvince->Clear();
       $this->cmbProvince->AddItem('--Select--', null, null);
       $result_province = mysql_query("select * from lookup_province where region_id='".$region_id."'");

       while($row_province = mysql_fetch_array($result_province))
       {
            $this->cmbProvince->AddItem($row_province['province'], null, $row_province['id']);
       }

       $province_id = $row_company['province'];
       $this->cmbMunicipality->Clear();
       $this->cmbMunicipality->AddItem('--Select--', null, null);
       $result_municipality = mysql_query("select * from lookup_municipality where province_id='".$province_id."'");

       while($row_municipality = mysql_fetch_array($result_municipality))
       {
            $this->cmbMunicipality->AddItem($row_municipality['municipality'], null, $row_municipality['id']);
       }
        $this->cmbProvince->setItemIndex($row_company['province']);
        $this->cmbMunicipality->setItemIndex($row_company['municipality']);


       }

}

global $application;

global $register_company_edit2;

//Creates the form
$register_company_edit2=new register_company_edit2($application);

//Read from resource file
$register_company_edit2->loadResource(__FILE__);

//Shows the form
$register_company_edit2->show();

?>